package com.usian.utils.common;

import com.alibaba.fastjson.JSON;
import com.aliyun.imageaudit20191230.models.ScanTextResponse;
import com.aliyun.imageaudit20191230.models.ScanTextResponseBody;
import com.aliyun.tea.TeaException;
import lombok.extern.slf4j.Slf4j;

import java.util.List;

/**
 * @author baimugudu
 * @email 2415621370@qq.com
 * @date 2023/8/30 9:53
 */
@Slf4j
public class ScanTextUtil {


    public static final String accessKeyId = "LTAI5t9AAEksbQkcnzMQ3uQ5";
    public static final String accessKeySecret = "KoNtA0ld6DmlxLHfBD2fR3u8bCKyS2";
    /**
     * 使用AK&SK初始化账号Client
     * @param accessKeyId
     * @param accessKeySecret
     * @return Client
     * @throws Exception
     */
    public static com.aliyun.imageaudit20191230.Client createClient(String accessKeyId, String accessKeySecret) throws Exception {
        com.aliyun.teaopenapi.models.Config config = new com.aliyun.teaopenapi.models.Config()
                // 必填，您的 AccessKey ID
                .setAccessKeyId(accessKeyId)
                // 必填，您的 AccessKey Secret
                .setAccessKeySecret(accessKeySecret);
        // Endpoint 请参考 https://api.aliyun.com/product/imageaudit
        config.endpoint = "imageaudit.cn-shanghai.aliyuncs.com";
        return new com.aliyun.imageaudit20191230.Client(config);
    }


    public static boolean scanText(String keyword) throws Exception {

        // 请确保代码运行环境设置了环境变量 ALIBABA_CLOUD_ACCESS_KEY_ID 和 ALIBABA_CLOUD_ACCESS_KEY_SECRET。
        // 工程代码泄露可能会导致 AccessKey 泄露，并威胁账号下所有资源的安全性。以下代码示例使用环境变量获取 AccessKey 的方式进行调用，仅供参考，建议使用更安全的 STS 方式，更多鉴权访问方式请参见：https://help.aliyun.com/document_detail/378657.html
        com.aliyun.imageaudit20191230.Client client = ScanTextUtil.createClient(accessKeyId, accessKeySecret);
        com.aliyun.imageaudit20191230.models.ScanTextRequest.ScanTextRequestLabels labels0 = new com.aliyun.imageaudit20191230.models.ScanTextRequest.ScanTextRequestLabels()
                .setLabel("spam");
        com.aliyun.imageaudit20191230.models.ScanTextRequest.ScanTextRequestLabels labels1 = new com.aliyun.imageaudit20191230.models.ScanTextRequest.ScanTextRequestLabels()
                .setLabel("politics");
        com.aliyun.imageaudit20191230.models.ScanTextRequest.ScanTextRequestLabels labels2 = new com.aliyun.imageaudit20191230.models.ScanTextRequest.ScanTextRequestLabels()
                .setLabel("abuse");
        com.aliyun.imageaudit20191230.models.ScanTextRequest.ScanTextRequestLabels labels3 = new com.aliyun.imageaudit20191230.models.ScanTextRequest.ScanTextRequestLabels()
                .setLabel("terrorism");
        com.aliyun.imageaudit20191230.models.ScanTextRequest.ScanTextRequestLabels labels4 = new com.aliyun.imageaudit20191230.models.ScanTextRequest.ScanTextRequestLabels()
                .setLabel("porn");
        com.aliyun.imageaudit20191230.models.ScanTextRequest.ScanTextRequestLabels labels5 = new com.aliyun.imageaudit20191230.models.ScanTextRequest.ScanTextRequestLabels()
                .setLabel("flood");
        com.aliyun.imageaudit20191230.models.ScanTextRequest.ScanTextRequestLabels labels6 = new com.aliyun.imageaudit20191230.models.ScanTextRequest.ScanTextRequestLabels()
                .setLabel("contraband");
        com.aliyun.imageaudit20191230.models.ScanTextRequest.ScanTextRequestLabels labels7 = new com.aliyun.imageaudit20191230.models.ScanTextRequest.ScanTextRequestLabels()
                .setLabel("ad");
        com.aliyun.imageaudit20191230.models.ScanTextRequest.ScanTextRequestLabels labels8 = new com.aliyun.imageaudit20191230.models.ScanTextRequest.ScanTextRequestLabels()
                .setLabel("contraband");
        com.aliyun.imageaudit20191230.models.ScanTextRequest.ScanTextRequestTasks tasks0 = new com.aliyun.imageaudit20191230.models.ScanTextRequest.ScanTextRequestTasks()
                .setContent(keyword);
        com.aliyun.imageaudit20191230.models.ScanTextRequest scanTextRequest = new com.aliyun.imageaudit20191230.models.ScanTextRequest()
                .setTasks(java.util.Arrays.asList(
                        tasks0
                ))
                .setLabels(java.util.Arrays.asList(
                        labels0,
                        labels1,
                        labels2,
                        labels3,
                        labels4,
                        labels5,
                        labels6,
                        labels7,
                        labels8
                ));
        com.aliyun.teautil.models.RuntimeOptions runtime = new com.aliyun.teautil.models.RuntimeOptions();
        try {
            // 复制代码运行请自行打印 API 的返回值
            ScanTextResponse scanTextResponse = client.scanTextWithOptions(scanTextRequest, runtime);
            log.info("scanTextResponse:{}", JSON.toJSONString(scanTextResponse));
            ScanTextResponseBody.ScanTextResponseBodyData data = scanTextResponse.getBody().getData();
            List<ScanTextResponseBody.ScanTextResponseBodyDataElements> elements = data.getElements();
            for (int i = 0; i < elements.size(); i++) {
                ScanTextResponseBody.ScanTextResponseBodyDataElements scanTextResponseBodyDataElements = elements.get(i);
                List<ScanTextResponseBody.ScanTextResponseBodyDataElementsResults> results = scanTextResponseBodyDataElements.getResults();
                for (int i1 = 0; i1 < results.size(); i1++) {
                    ScanTextResponseBody.ScanTextResponseBodyDataElementsResults scanTextResponseBodyDataElementsResults = results.get(i1);
                    String suggestion = scanTextResponseBodyDataElementsResults.getSuggestion();
                    if(!suggestion.equals("pass")){
                        return false;
                    }
                }
            }

        } catch (TeaException error) {
            // 如有需要，请打印 error
            com.aliyun.teautil.Common.assertAsString(error.message);
        } catch (Exception _error) {
            TeaException error = new TeaException(_error.getMessage(), _error);
            // 如有需要，请打印 error
            com.aliyun.teautil.Common.assertAsString(error.message);
        }

        return true;

    }

    public static void main(String[] args_) throws Exception {
        List<String> args = java.util.Arrays.asList(args_);
        // 请确保代码运行环境设置了环境变量 ALIBABA_CLOUD_ACCESS_KEY_ID 和 ALIBABA_CLOUD_ACCESS_KEY_SECRET。
        // 工程代码泄露可能会导致 AccessKey 泄露，并威胁账号下所有资源的安全性。以下代码示例使用环境变量获取 AccessKey 的方式进行调用，仅供参考，建议使用更安全的 STS 方式，更多鉴权访问方式请参见：https://help.aliyun.com/document_detail/378657.html
        com.aliyun.imageaudit20191230.Client client = ScanTextUtil.createClient(accessKeyId, accessKeySecret);
        com.aliyun.imageaudit20191230.models.ScanTextRequest.ScanTextRequestLabels labels0 = new com.aliyun.imageaudit20191230.models.ScanTextRequest.ScanTextRequestLabels()
                .setLabel("spam");
        com.aliyun.imageaudit20191230.models.ScanTextRequest.ScanTextRequestLabels labels1 = new com.aliyun.imageaudit20191230.models.ScanTextRequest.ScanTextRequestLabels()
                .setLabel("politics");
        com.aliyun.imageaudit20191230.models.ScanTextRequest.ScanTextRequestLabels labels2 = new com.aliyun.imageaudit20191230.models.ScanTextRequest.ScanTextRequestLabels()
                .setLabel("abuse");
        com.aliyun.imageaudit20191230.models.ScanTextRequest.ScanTextRequestLabels labels3 = new com.aliyun.imageaudit20191230.models.ScanTextRequest.ScanTextRequestLabels()
                .setLabel("terrorism");
        com.aliyun.imageaudit20191230.models.ScanTextRequest.ScanTextRequestLabels labels4 = new com.aliyun.imageaudit20191230.models.ScanTextRequest.ScanTextRequestLabels()
                .setLabel("porn");
        com.aliyun.imageaudit20191230.models.ScanTextRequest.ScanTextRequestLabels labels5 = new com.aliyun.imageaudit20191230.models.ScanTextRequest.ScanTextRequestLabels()
                .setLabel("flood");
        com.aliyun.imageaudit20191230.models.ScanTextRequest.ScanTextRequestLabels labels6 = new com.aliyun.imageaudit20191230.models.ScanTextRequest.ScanTextRequestLabels()
                .setLabel("contraband");
        com.aliyun.imageaudit20191230.models.ScanTextRequest.ScanTextRequestLabels labels7 = new com.aliyun.imageaudit20191230.models.ScanTextRequest.ScanTextRequestLabels()
                .setLabel("ad");
        com.aliyun.imageaudit20191230.models.ScanTextRequest.ScanTextRequestLabels labels8 = new com.aliyun.imageaudit20191230.models.ScanTextRequest.ScanTextRequestLabels()
                .setLabel("contraband");
        com.aliyun.imageaudit20191230.models.ScanTextRequest.ScanTextRequestTasks tasks0 = new com.aliyun.imageaudit20191230.models.ScanTextRequest.ScanTextRequestTasks()
                .setContent("法轮功");
        com.aliyun.imageaudit20191230.models.ScanTextRequest scanTextRequest = new com.aliyun.imageaudit20191230.models.ScanTextRequest()
                .setTasks(java.util.Arrays.asList(
                        tasks0
                ))
                .setLabels(java.util.Arrays.asList(
                        labels0,
                        labels1,
                        labels2,
                        labels3,
                        labels4,
                        labels5,
                        labels6,
                        labels7,
                        labels8
                ));
        com.aliyun.teautil.models.RuntimeOptions runtime = new com.aliyun.teautil.models.RuntimeOptions();
        try {
            // 复制代码运行请自行打印 API 的返回值
            ScanTextResponse scanTextResponse = client.scanTextWithOptions(scanTextRequest, runtime);
            log.info("scanTextResponse:{}", JSON.toJSONString(scanTextResponse));
        } catch (TeaException error) {
            // 如有需要，请打印 error
            com.aliyun.teautil.Common.assertAsString(error.message);
        } catch (Exception _error) {
            TeaException error = new TeaException(_error.getMessage(), _error);
            // 如有需要，请打印 error
            com.aliyun.teautil.Common.assertAsString(error.message);
        }
    }
}
